package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.entity.DecorationKeyword;
import com.ruoyi.mapper.DecorationKeywordMapper;
import com.ruoyi.service.DecorationKeywordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 装饰画关键词关联服务实现类
 *
 * @author ruoyi
 */
@Service
@Slf4j
public class DecorationKeywordServiceImpl extends ServiceImpl<DecorationKeywordMapper, DecorationKeyword> implements DecorationKeywordService {

    /**
     * 根据装饰画ID获取关联的关键词列表
     *
     * @param decorationId 装饰画ID
     * @return 关联的关键词列表
     */
    @Override
    public List<DecorationKeyword> listKeywordsByDecorationId(Integer decorationId) {
        LambdaQueryWrapper<DecorationKeyword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationKeyword::getDecorationId, decorationId);
        return list(wrapper);
    }

    /**
     * 添加装饰画与关键词的关联
     *
     * @param decorationId 装饰画ID
     * @param keywordId    关键词ID
     * @return 是否成功添加关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addKeyword(Integer decorationId, Integer keywordId) {
        // 先查询是否已存在关联
        LambdaQueryWrapper<DecorationKeyword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationKeyword::getDecorationId, decorationId)
                .eq(DecorationKeyword::getKeywordId, keywordId);
        DecorationKeyword existing = getOne(wrapper);
        if (existing != null) {
            log.info("装饰画与关键词关联已存在，decorationId={}，keywordId={}", decorationId, keywordId);
            return true;
        }

        // 创建新关联
        DecorationKeyword decorationKeyword = new DecorationKeyword();
        decorationKeyword.setDecorationId(decorationId);
        decorationKeyword.setKeywordId(keywordId);
        return save(decorationKeyword);
    }

    /**
     * 移除装饰画与关键词的关联
     *
     * @param decorationId 装饰画ID
     * @param keywordId    关键词ID
     * @return 是否成功移除关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeKeyword(Integer decorationId, Integer keywordId) {
        LambdaQueryWrapper<DecorationKeyword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationKeyword::getDecorationId, decorationId)
                .eq(DecorationKeyword::getKeywordId, keywordId);
        return remove(wrapper);
    }

    /**
     * 批量添加装饰画与关键词的关联
     *
     * @param decorationId 装饰画ID
     * @param keywordIds   关键词ID列表
     * @return 是否成功批量添加关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddKeywords(Integer decorationId, List<Integer> keywordIds) {
        if (keywordIds == null || keywordIds.isEmpty()) {
            return true;
        }

        // 查询现有关联
        LambdaQueryWrapper<DecorationKeyword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationKeyword::getDecorationId, decorationId)
                .in(DecorationKeyword::getKeywordId, keywordIds);
        List<DecorationKeyword> existingKeywords = list(wrapper);

        // 过滤出需要新增的关键词ID
        List<Integer> existingKeywordIds = existingKeywords.stream()
                .map(DecorationKeyword::getKeywordId)
                .collect(java.util.stream.Collectors.toList());

        List<DecorationKeyword> newKeywords = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();

        for (Integer keywordId : keywordIds) {
            if (!existingKeywordIds.contains(keywordId)) {
                DecorationKeyword decorationKeyword = new DecorationKeyword();
                decorationKeyword.setDecorationId(decorationId);
                decorationKeyword.setKeywordId(keywordId);
                newKeywords.add(decorationKeyword);
            }
        }

        return newKeywords.isEmpty() || saveBatch(newKeywords);
    }

    /**
     * 批量移除装饰画与关键词的关联
     *
     * @param decorationId 装饰画ID
     * @param keywordIds   关键词ID列表
     * @return 是否成功批量移除关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemoveKeywords(Integer decorationId, List<Integer> keywordIds) {
        if (keywordIds == null || keywordIds.isEmpty()) {
            return true;
        }

        LambdaQueryWrapper<DecorationKeyword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DecorationKeyword::getDecorationId, decorationId)
                .in(DecorationKeyword::getKeywordId, keywordIds);
        return remove(wrapper);
    }
}
